/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.der.almo.util;


import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author Rodrigo Fujioka
 */
public class jpaUtil {

    private static EntityManagerFactory entityManagerFactory;
    private static EntityManager em;

    private jpaUtil() {
        
    }

    public static EntityManagerFactory getEntityManagerFactory() {

        if (entityManagerFactory == null) {
            try {
              
            	entityManagerFactory = 
            			Persistence.createEntityManagerFactory("persistence");

            } catch (Throwable ex) {
                // Log the exception.
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }

            return entityManagerFactory;

        } else {
            return entityManagerFactory;
        }
        
    }
    
    
    
    
    /**
     *  Retorna uma inst�ncia de EntityManager utlizada
     *  para persistir objeto nos DAOS do projetos.     *  
     * @return EntityManager uma inst�ncia de EntityManager.
     */

    public static EntityManager getEntityManager(){
    	return getEntityManagerFactory().createEntityManager();
    }

}
